$(function () {

    layui.use('form', function () {
        var form = layui.form;

        form.on('submit(register)', function (data) {

            console.log("登录注册的数据:",data.field);

            $.ajax({
                url: 'http://localhost:8080/back/user/insertUser',
                data: JSON.stringify(data.field),
                contentType:'application/json',
                type: 'post',
                dataType:'json',
                success: function (res) {
                    console.log("后端返回de数据", res)
                    if (res.msg == "success") {
                        //发异步，把数据提交给php
                        layer.alert("注册成功", {
                                icon: 6
                            },
                            function () {
                                location.href = "http://localhost:63342/front_page/html/login.html"
                            });
                    } else {
                        layer.alert("注册失败");
                    }
                }
            })
            return false;
        });

        form.verify({
            r_username:[/^[A-z]/,"名字必须以字母开头"],
            r_password:[/(.+){6,8}/,"密码长度6到12位"],
            r_repass: function (value) {
                if ($('#R_PASS').val() != $('#R_REPASS').val()) {
                    return '两次密码不一致';
                }
            }
        });


    });
})

function display(obj) {
    //当前是输入密码后面的图片
    if(obj.id == "pass_img"){
        //当前输入框类型是密码
        if($("#R_PASS").attr("type") == "password"){
            $("#R_PASS").attr("type","text")
            $(obj).attr("src","../img/open.png")
        }
        //当前输入框类型是文本
        else{
            $("#R_PASS").attr("type","password")
            $(obj).attr("src","../img/close.png")
        }

    }
    //当前是确认密码后面的图片
    else {
        //当前确认输入框类型是密码
        if($("#R_REPASS").attr("type") == "password"){
            $("#R_REPASS").attr("type","text")
            $(obj).attr("src","../img/open.png")
        }
        //当前确认输入框类型是文本
        else {
            $("#R_REPASS").attr("type","password")
            $(obj).attr("src","../img/close.png")
        }
    }
}



